package com.example.jmxdemo;

import com.example.jmxdemo.jmx.HelloDynamicMBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.stereotype.Component;

import javax.management.MBeanServer;
import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;
import java.lang.management.ManagementFactory;

@SpringBootApplication
public class JxmDemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(JxmDemoApplication.class, args);
    }

    @Component
    public static class AppRunner implements ApplicationRunner {

        //        @Autowired
//        private WelcomeConfigMBean welcomeConfigMbean;
        @Autowired
        private MBeanServer mBeanServer;

        @Override
        public void run(ApplicationArguments args) throws Exception {
//            ObjectName objectName = new ObjectName("com.example.jmxdemo.jmx:type=WelcomeConfigMbean");
//            mBeanServer.registerMBean(welcomeConfigMbean, objectName);

            ObjectName objectName = new ObjectName("com.example.jmxdemo.jmx:type=" + HelloDynamicMBean.name);
            mBeanServer.registerMBean(new HelloDynamicMBean(), objectName);

            long currentThreadCpuTime = ManagementFactory.getThreadMXBean().getCurrentThreadCpuTime();

//            ManagementFactory.getThreadMXBean()
        }

        private void registerOpenMBean() throws MalformedObjectNameException {
//            ObjectName name = new ObjectName("com.example:type=MyOpenMbean");
//            OpenMBeanInfo mbeanInfo = createOpenMBeanInfo();
//            OpenMBeanInfoSupport openMBean = new OpenMBeanInfoSupport(...);
//            mBeanServer.registerMBean(openMBean, name);
        }

    }
}
